Fk:loadTranslationTable{
  ["plum__niebo"] = "孽波",
  [":plum__niebo"] = "回合技，你成为当前回合角色使用黑色牌的唯一目标时，你可取消之，然后其可将两张基本牌当【万箭齐发】使用。若等效于发动“乱击”，你不能响应且可取消一个其他目标。",

  ["#plum__niebo-ivk"] = "孽波：%src 对你使用了 %arg，可取消你为目标",
  ["#plum__niebo-luanji"] = "孽波：可将两张基本牌当【万箭齐发】使用",
  ["#plum__niebo-cancel"] = "孽波：可为 %src 使用的 %arg 取消一个其他目标",

  ["#plum__niebo-cancelOne"] = "%from 使用了〖<font color='#0598BC'><b>孽波</b></font>〗，取消了 %arg 的目标 %to"
}

local T = require "packages.tuntian_studio.utility"

local niebo = fk.CreateSkill{
  name = "plum__niebo",
  tags = {"Turn"},
}

niebo:addEffect(fk.TargetConfirmed, {
  anim_type = "defensive",
  can_trigger = function (self, event, target, player, data)
    if player == target and player:hasSkill(niebo.name) and player:usedSkillTimes(niebo.name, Player.HistoryTurn) == 0 then
      return data.from == player.room.current and data.card.color == Card.Black and #data:getAllTargets() == 1
    end
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = niebo.name,
      prompt = "#plum__niebo-ivk:" .. data.from.id .. "::" .. data.card:toLogString()
    })
  end,
  on_use = function (self, event, target, player, data)
    data:cancelAllTarget()
    data.extra_data = data.extra_data or {}
    data.extra_data.plum__niebo = data.extra_data.plum__niebo or {}
    data.extra_data.plum__niebo[player.id] = true
  end
})

niebo:addEffect(fk.CardUseFinished, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    return player == target and data.extra_data and data.extra_data.plum__niebo
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local _, ret = room:askToUseActiveSkill(player, {
      skill_name = "plum__niebo_luanji",
      prompt = "#plum__niebo-luanji"
    })
    if not ret or #ret.cards ~= 2 then return end
    local j = Fk:getCardById(ret.cards[1]).suit == Fk:getCardById(ret.cards[2]).suit
    local card = Fk:cloneCard(T.cardFix("archery_attack"))
    card.skillName = "plum__niebo"
    card:addSubcards(ret.cards)
    local use_data = { ---@type UseCardDataSpec
      from = player,
      card = card,
      tos = card:getFixedTargets(player),
    }
    event:setCostData(self, { use_data = use_data, equalToLuanji = j })
    return true
  end,
  on_use = function (self, event, target, player, data)
    local use_data = event:getCostData(self).use_data ---@type UseCardDataSpec
    local room = player.room
    use_data.extra_data = {
      disresponsiveList = table.filter(room:getAlivePlayers(true), function(p) return data.extra_data.plum__niebo[p.id] end),
      equalToLuanji = event:getCostData(self).equalToLuanji,
    }
    room:useCard(use_data)
  end
})

niebo:addEffect(fk.CardUsing, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function (self, event, target, player, data)
    if data.card.skillName == niebo.name and data.extra_data.equalToLuanji then
      return table.contains(data.extra_data.disresponsiveList, player)
    end
  end,
  on_trigger = function (self, event, target, player, data)
    data.disresponsiveList = data.disresponsiveList or {}
    table.insertIfNeed(data.disresponsiveList, player)
    self:doCost(event, target, player, data)
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local tos = room:askToChoosePlayers(player, {
      targets = table.filter(data:getAllTargets(), function(p) return p ~= player end),
      min_num = 1,
      max_num = 1,
      skill_name = niebo.name,
      cancelable = true,
      prompt = "#plum__niebo-cancel:" .. target.id .. "::" .. data.card:toLogString()
    })
    if #tos == 0 then return end
    event:setCostData(self, {to = tos[1]})
    return true
  end,
  on_use = function (self, event, target, player, data)
    local to = event:getCostData(self).to
    local room = player.room
    data:removeTarget(to)
    room:sendLog{
      type = "#plum__niebo-cancelOne",
      toast = true,
      from = player.id,
      to = {to.id},
      arg = data.card:toLogString(),
    }
  end
})

return niebo